package com.iotechn.unimall.app.api.running;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.RunningRecordDO;
import com.iotechn.unimall.data.domain.UserDO;
import com.iotechn.unimall.data.dto.RunningRecordDTO;
import com.iotechn.unimall.data.enums.RunningStatus;
import com.iotechn.unimall.data.mapper.RunningRecordMapper;
import com.iotechn.unimall.data.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by rize on 2019/6/30.
 */
@Service
public class RunningRecordServiceImpl implements RunningRecordService {

    private static final String VERIFY_CODE_PREFIX = "VERIFY_CODE_";

    private static final Logger logger = LoggerFactory.getLogger(RunningRecordServiceImpl.class);

    @Autowired
    private RunningRecordMapper runningRecordMapper;
    @Autowired
    private UserMapper userMapper;

    private static String CHINESE_DATE_FORMAT= "yyyy年MM月dd日 HH点mm分";


    @Override
    public Page<RunningRecordDTO> selectPage(Long userId, Integer pageNo, Integer pageSize) {
        Long runningGroupId = -1L;
        UserDO userDO = userMapper.selectById(userId);
        if(userDO!=null){
            runningGroupId = userDO.getRunningGroupId();
        }

        Page<RunningRecordDTO> page=new Page<>(pageNo,pageSize);
        List<RunningRecordDTO> list = runningRecordMapper.selectPage(page, DateUtil.beginOfMonth(new Date()), DateUtil.endOfMonth(new Date()),runningGroupId);

        for(RunningRecordDTO dto:list){
            String format = DateUtil.format(dto.getGmtCreate(), CHINESE_DATE_FORMAT);
            dto.setGmtCreateStr(format);
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public Boolean addRecord(Long userId, String picUrl) throws ServiceException {
        Long runningGroupId = -1L;
        UserDO userDO = userMapper.selectById(userId);
        if(userDO!=null){
            runningGroupId = userDO.getRunningGroupId();
        }

        Integer recordNum = runningRecordMapper.selectCount(new QueryWrapper<RunningRecordDO>()
                .eq("is_del",0)
                .eq("user_id", userId)
                .eq("running_group_id",runningGroupId)
//                .ge("gmt_create", DateUtil.beginOfMonth(new Date()))
//                .le("gmt_create",DateUtil.endOfMonth(new Date()))
        );

        RunningRecordDO runningRecordDO=new RunningRecordDO();
        runningRecordDO.setUserId(userId);
        runningRecordDO.setPicUrl(picUrl);
        runningRecordDO.setStatus(RunningStatus.NO_CHECK.getCode());
        runningRecordDO.setGmtCreate(new Date());
        runningRecordDO.setGmtUpdate(new Date());
        runningRecordDO.setRunningGroupId(runningGroupId);


        if(recordNum>0){
            runningRecordMapper.update(runningRecordDO
                    , new QueryWrapper<RunningRecordDO>()
                            .eq("is_del",0)
                            .eq("user_id", userId)
                            .eq("running_group_id",runningGroupId)
//                            .ge("gmt_create", DateUtil.beginOfMonth(new Date()))
//                            .le("gmt_create",DateUtil.endOfMonth(new Date()))
            );
        }else{
            runningRecordMapper.insert(runningRecordDO);
        }

        return true;
    }

    @Override
    public Boolean approvalRecord(Long recordId, Integer status, Long userId) throws ServiceException {
        RunningRecordDO runningRecordDO=new RunningRecordDO();
        runningRecordDO.setId(recordId);
        runningRecordDO.setCheckUserId(userId);
        runningRecordDO.setStatus(status);
        runningRecordDO.setGmtUpdate(new Date());

        runningRecordMapper.update(runningRecordDO //更新数据
                , new QueryWrapper<RunningRecordDO>()
                        .eq("id",recordId)
                        .eq("status", RunningStatus.NO_CHECK.getCode())
        );
        return true;
    }

    @Override
    public Boolean deleteRecord(Long recordId, Long userId) throws ServiceException {
        this.runningRecordMapper.deleteById(recordId);
        return Boolean.valueOf(true);
    }

    @Override
    public Boolean cancelRecord(Long recordId, Long userId) throws ServiceException {
        RunningRecordDO runningRecordDO = new RunningRecordDO();
        runningRecordDO.setId(recordId);
        runningRecordDO.setStatus(Integer.valueOf(RunningStatus.NO_CHECK.getCode()));
        this.runningRecordMapper.updateById(runningRecordDO);
        return Boolean.valueOf(true);
    }

    @Override
    public Boolean clealAllData(Long userId) throws ServiceException {
        Long runningGroupId = -1L;
        UserDO userDO = userMapper.selectById(userId);
        if(userDO!=null){
            runningGroupId = userDO.getRunningGroupId();
        }
        this.runningRecordMapper.deleteAllRecord(runningGroupId);
        return Boolean.valueOf(true);
    }

    @Override
    public Boolean showApplyButton() throws ServiceException {
        UserDO userDO = userMapper.selectById(Integer.valueOf(456));
        if (userDO != null && userDO.getLevel().intValue() > 3) {
            return Boolean.valueOf(false);
        }
        return Boolean.valueOf(true);
    }


    public static void main(String[] args) {
        String format = DateUtil.format(new Date(), CHINESE_DATE_FORMAT);
        System.out.println("====================:"+format);
    }

}
